package fr.upstlse.glre.client.espaceperso.component;

import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.constante.EspaceEtudiantConstante;
import fr.upstlse.glre.client.constante.RfgConstanteEnum;
import fr.upstlse.glre.client.espacededie.commun.communiquer.Message;
import fr.upstlse.glre.client.espacededie.commun.depot.Depot;
import fr.upstlse.glre.client.espacededie.commun.depot.Telecharger;
import fr.upstlse.glre.client.espacededie.component.AbstractMenu;
import fr.upstlse.glre.client.espaceperso.EspaceEtudiant;
import fr.upstlse.glre.client.espaceperso.depot.action.EtudiantViewerSelectionAction;
import fr.upstlse.glre.client.espaceperso.profil.EtudiantProfil;
import fr.upstlse.glre.client.espaceperso.profil.EtudiantStage;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vitrine.Vitrine;

public class WestPanel extends AbstractMenu {

	private TabPanel centerTabPanel;

	private void addAComponentToNavPanel(Widget comp) {
		add(comp);
	}

	private void createDepotNav() {
		// ajout du dépot
		Label lDepot = new Label(RfgConstante.DEPOT_NAV_LABEL);
		lDepot.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				TabItem item = new TabItem();
				item.setScrollMode(Scroll.AUTO);
				item.setClosable(true);
				item.setText(RfgConstante.DEPOT_TABITEM_TEXT);
				item.setIconStyle("icon-tabs");
				item.setId(RfgConstante.DEPOT_TABITEM_ID);
				centerTabPanel.add(item);
				centerTabPanel.setSelection(item);

				item.add(new Depot());
				Telecharger lTelecharger = new Telecharger();
				lTelecharger.setViewerSelectionAction(new EtudiantViewerSelectionAction(lTelecharger.getTelechargerPanel()));
				item.add(lTelecharger);
				item.layout();
			}
		});
		addAComponentToNavPanel(lDepot);
	}

	private void createDeconnexionMenu() {
		Label lDeconnexion = new Label(RfgConstante.DECONNEXION_LABEL);
		lDeconnexion.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				EspaceEtudiant.killInstance();
				RootPanel.get().clear();
				Vitrine lVitrine = new Vitrine();
			    RootPanel.get().add(lVitrine.getPanel());
			    lVitrine.loadVitrine();
			}
		});
		addAComponentToNavPanel(lDeconnexion);
	}
	
	private void createEmploiDuTempsMenu() {
		// ajout de l'emploi du temps
		Label lEdtLabel = new Label(EspaceEtudiantConstante.MENU_EMPLOI_DU_TEMPS_LABEL.getValue());
		lEdtLabel.addClickListener(new ClickListener() {
			public void onClick(Widget arg0) {
				final TabItem lItem = new TabItem();
				lItem.setScrollMode(Scroll.AUTO);
				lItem.setClosable(true);
				lItem.setIconStyle("icon-tabs");
				lItem.setText(EspaceEtudiantConstante.TAB_ITEM_EMPLOI_DU_TEMPS_TEXT.getValue());
				lItem.setId(EspaceEtudiantConstante.TAB_ITEM_EMPLOI_DU_TEMPS_ID.getValue());
				AsyncCallback<String> lAsyncCallback = new AsyncCallback<String>() {
					public void onFailure(Throwable arg0) {
						centerTabPanel.add(lItem);
						centerTabPanel.setSelection(lItem);
						lItem.add(new Html(arg0.getMessage()));
						lItem.layout();
					}

					public void onSuccess(String arg0) {
						centerTabPanel.add(lItem);
						centerTabPanel.setSelection(lItem);
						lItem.add(new Html(arg0));
						lItem.layout();
					}
				};
				ControlerUtil.getControlerServiceAsyncInstance().getEmploiDuTemps(lAsyncCallback);
			}
		});
		addAComponentToNavPanel(lEdtLabel);
	}
	
	private void createProfilMenu() {
		// ajout du profil
		Label lEdtLabel = new Label(EspaceEtudiantConstante.MENU_PROFIL_LABEL.getValue());
		lEdtLabel.addClickListener(new ClickListener() {
			public void onClick(Widget arg0) {
				final TabItem lItem = new TabItem();
				lItem.setScrollMode(Scroll.AUTO);
				lItem.setClosable(true);
				lItem.setIconStyle("icon-tabs");
				lItem.setText(EspaceEtudiantConstante.TAB_ITEM_PROFIL_TEXT.getValue());
				lItem.setId(EspaceEtudiantConstante.TAB_ITEM_PROFIL_ID.getValue());
						centerTabPanel.add(lItem);
						centerTabPanel.setSelection(lItem);
						
						EtudiantProfil lEtudiantProfil = new EtudiantProfil();
						lItem.add(lEtudiantProfil);
						if(EspaceEtudiant.getInstance().getUser().getStage() != null) {
							lItem.add(new EtudiantStage());
						}
						lItem.layout();
			}
		});
		addAComponentToNavPanel(lEdtLabel);
	}
	
	private void createMessageMenu() {
		// ajout de message
		Label lEdtLabel = new Label(RfgConstanteEnum.MENU_MESSAGE_LABEL
				.getValue());
		lEdtLabel.addClickListener(new ClickListener() {
			public void onClick(Widget arg0) {
				final TabItem lItem = new TabItem();
				lItem.setScrollMode(Scroll.AUTO);
				lItem.setClosable(true);
				lItem.setIconStyle("icon-tabs");
				lItem
						.setText(RfgConstanteEnum.TAB_ITEM_MESSAGE_TEXT
								.getValue());
				lItem.setId(RfgConstanteEnum.TAB_ITEM_MESSAGE_ID.getValue());
				lItem.setLayout(new FitLayout());
				centerTabPanel.add(lItem);
				centerTabPanel.setSelection(lItem);

				Message lMessage = new Message();
				lMessage.setUserVOGWT(EspaceEtudiant.getInstance().getUser());
				lItem.add(lMessage);
				lItem.layout();
			}
		});
		addAComponentToNavPanel(lEdtLabel);
	}

	public void initWidget() {
		setTitle("Naviguer sur le site");

		centerTabPanel = CenterPanel.getInstance();

		createProfilMenu();
		createMessageMenu();
		
		// ajout de l'agenda
		Label agenda = new Label("Agenda");
		agenda.addClickListener(new ClickListener() {
			public void onClick(Widget arg0) {
				TabItem item = new TabItem();
				item.setScrollMode(Scroll.AUTO);
				item.setClosable(true);
				item.setText("GWT");
				item.setIconStyle("icon-tabs");
				item.setId("Agenda");
				centerTabPanel.add(item);
				centerTabPanel.setSelection(item);
				item
						.setUrl("http://www.google.com/calendar/embed?showTitle=0&amp;height=600&amp;wkst=2&amp;hl=fr&amp;bgcolor=%23FFFFFF&amp;src=nmk17icclf9ocn88pf4o0omol4%40group.calendar.google.com&amp;color=%23B1365F");
			}
		});
		addAComponentToNavPanel(agenda);

		// ajout du forum
		Label forum = new Label("Forum");
		forum.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				TabItem item = new TabItem();
				item.setScrollMode(Scroll.AUTO);
				item.setClosable(true);
				item.setText("Forum");
				item.setIconStyle("icon-tabs");
				item.setId("Forum");
				centerTabPanel.add(item);
				centerTabPanel.setSelection(item);

				item.add(new Label("Forum"));
			}
		});
		addAComponentToNavPanel(forum);

		createEmploiDuTempsMenu();
		createDepotNav();
		createDeconnexionMenu();
	}

}
